﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DLL;

public partial class examing : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["examNum"] == null)
            return;
        if (!IsPostBack)
        {
            //获取数据
            BindST();
            //考试结束时间
            labTimeEnd.Text = (DateTime.Now.AddMinutes(double.Parse(((Label)fvPaperInfo.FindControl("labTimeOver")).Text))).ToLongTimeString();
            //考试开始时间
            labTimeStart.Text = DateTime.Now.ToLongTimeString();
        }
        viewIsNoData();
    }

    //获取数据
    private void BindST()
    {
        //接收页面传值试卷编号
        string examNum = Request.QueryString["examNum"];
        //根据试卷编号获取试卷信息
        gvExistQues.DataSource = ques_BLL.GetQuesSource(examNum);
        gvExistQues.DataBind();
        //根据试卷编号获取试卷题目
        fvPaperInfo.DataSource = examDesign_BLL.GetSource(examNum);
        fvPaperInfo.DataBind();
    }

    #region 脚本
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvExistQues.Rows.Count == 0)
        {
            RegisterStartupScript("prompt2", "<script type='text/javascript'>document.getElementById('divQues').innerHTML='(尚未找到相关数据)'</script>");
        }
    }
    /// <summary>
    /// 刷新当前页
    /// </summary>
    private void RefurPage()
    {
        RegisterStartupScript("refur", "<script type='text/javascript'>location.replace(location.href)</script>");
    }
    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="msg">消息内容</param>
    protected void ShowMessage(string msg)
    {
        RegisterStartupScript("msg", "<script type='text/javascript' language='javascript'>alert('" + msg + "')</script>");
    }
    #endregion

    //提交试卷
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        //答题得分
        int n = 0;
        foreach (GridViewRow row in gvExistQues.Rows)
        {
            //获取用户答案控件
            CheckBoxList chklst = (CheckBoxList)row.FindControl("chklstDA");
            //获取正确答案控件
            Label labDA = (Label)row.FindControl("labDA");
            //获取每题分值控件
            Label labSingle = (Label)row.FindControl("labSingle");

            string userDA = "";
            //获取用户答案
            foreach (ListItem ite in chklst.Items)
            {
                if (ite.Selected)
                    userDA += ite.Value;
            }
            //判断用户答案
            if (labDA.Text == userDA)
            {
                //给正确答案加分
                n += int.Parse(labSingle.Text);
            }
        }
        //建立新的成绩数据对象并赋值
        CJ newCJ = new CJ();
        newCJ.cj = (double)n;
        newCJ.date = DateTime.Now.Date;
        newCJ.kmNum = decimal.Parse(((Label)fvPaperInfo.FindControl("labJB")).Text);
        newCJ.xsId = ((landInfo)Session["landInfo"]).UserId;
        //保存成绩信息
        bool bl = cj_BLL.SaveCJ(newCJ);
        if (bl)
            ShowMessage("成绩信息保存成功！您的本次考试成绩为" + n.ToString() + "分。");
        else
            ShowMessage("成绩信息保存失败！您的本次考试成绩为" + n.ToString() + "分。");
    }

    //计时器
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        //当前时间
        labTimeNow.Text = DateTime.Now.ToLongTimeString();
        if (DateTime.Now.ToLongTimeString() == labTimeEnd.Text)
        {
            Timer1.Enabled = false;
            labTimeNow.Text = "考试时间到！";
            gvExistQues.Enabled = false;
        }
    }
    //退出
    protected void btnExit_Click(object sender, EventArgs e)
    {
        Response.Redirect("home.aspx");
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        //答题得分
        int n = 0;
        foreach (GridViewRow row in gvExistQues.Rows)
        {
            //获取用户答案控件
            CheckBoxList chklst = (CheckBoxList)row.FindControl("chklstDA");
            //获取正确答案控件
            Label labDA = (Label)row.FindControl("labDA");
            //获取每题分值控件
            Label labSingle = (Label)row.FindControl("labSingle");

            string userDA = "";
            //获取用户答案
            foreach (ListItem ite in chklst.Items)
            {
                if (ite.Selected)
                    userDA += ite.Value;
            }
            //判断用户答案
            if (labDA.Text == userDA)
            {
                //给正确答案加分
                n += int.Parse(labSingle.Text);
            }
        }
        ShowMessage("您的本次考试成绩为 " + n.ToString() + " 分！");
    }
}
